package ae.pcfc.cldos.online.common.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Util Class for various Utility methods
 * Note : Please do not put methods without the consent of author
 * @author ammar
 * 
 */

public class UtilCommon {
	
	
	public static Date getDateFromString(String date){
		
		try {
			SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			return df.parse(date);	
		}catch(ParseException ex){
			return null;
		}
	}
	public static Date getDateFromStringFormat(String date){
		
		try {
			SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			return df.parse(date);	
		}catch(ParseException ex){
			return null;
		}
	}
	
	public static Date getDateFromStringWithTS(String date){
		
		try {
			SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
			return df.parse(date);	
		}catch(ParseException ex){
				return null;
		}

	}
	
	public static String getDefaultPaymentAdviceExpiryDate(){
		Calendar now = Calendar.getInstance();
		now.add(Calendar.MONTH,6);
		return UtilCommon.getDDMMYYYFromDate(now.getTime());
		
	}
	
	
	public static String getCurrentDateAsDDMMYYYY(){

		Date date = new Date();
		// Keep the MM upper case
		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		String formattedDate = df.format(date);
		return formattedDate;

	}
	
	public static String getDDMMYYYFromDate(Date date){
		try{
			SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			String formattedDate = df.format(date);
			return formattedDate;
		}catch(Exception e){
			return "";
		}
		
		
	}
	
	public static boolean dateCompare(String strStartDate, String strEndDate, String includeEndDate){

		try{
			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			
			Date startDate = formatter.parse(strStartDate);
			Date endDate = formatter.parse(strEndDate);
			
			if (includeEndDate == "N" && endDate.compareTo(startDate) < 0){
				return true;
			}else if(includeEndDate == "Y" && endDate.compareTo(startDate) <= 0){
				return true;
			}else 
				return false;
			
			
		}catch(Exception e){
			return true;
		}
	}
	
	
	
	public static boolean dateCompare(String strGivenDate, String includeEndDate){

		try{
			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			Date givenDate = formatter.parse(strGivenDate);
			Date currentDate = new Date();
			String strCurrentDate = formatter.format(currentDate);
			Date date = formatter.parse(strCurrentDate);
			
			if (includeEndDate == "N" && givenDate.compareTo(date) < 0){
				return true;
			}else if(includeEndDate == "Y" && givenDate.compareTo(date) <= 0){
				return true;
			}else 
				return false;
			
			
		}catch(Exception e){
			return true;
		}
	}
	
	public static Long convertStringToLong(String str) {
		return Long.parseLong(str);
	}
	
	public static Long convertBigDecimalToLong(BigDecimal str) {
		return str.longValue();
	}
}
	
	
